package Page.PersonalInformationPage;

import Controller.MainController;
import Controller.User.TeacherUser;
import Dao.TeacherDao;
import DataClass.Teacher;
import Navigation.NavigationController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;

import Page.MainPage.MainPageViewModel;

import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;

public class TeacherPersonalInformationController implements Initializable {
    @FXML
    public TextField TnoField;
    @FXML
    public TextField TCNameField;
    @FXML
    public TextField TSexField;
    @FXML
    public TextField JTnoField;
    @FXML
    public TextField TQualificationField;
    @FXML
    public TextField TTnoField;
    @FXML
    public TextField TDutiesField;
    @FXML
    public TextField TSituationField;
    @FXML
    public TextField TEducationField;
    @FXML
    public TextField TFirstDisciplineField;
    @FXML
    public TextField TSecondDisciplineField;
    @FXML
    public TextField TProfessionalAffiliationsField;
    @FXML
    public TextField TAdminPositionsCollegeField;
    @FXML
    public TextField TAcademicPositionsCollegeField;
    @FXML
    public TextField TRemarkField;
    @FXML
    public TextField TPasswordField;
    @FXML
    public Button EditButton;
    @FXML
    public Button LogoutButton;
    @FXML
    public Button SaveButton;
    @FXML
    private Button BackButton;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Teacher user;
        // 从登录控制器获取信息
        try {
            user =TeacherDao.getTeacher(((TeacherUser) MainController.getCurrentUser()).getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        // 初始化和设置文本
        TnoField.setText(user.getTno());
        TnoField.setEditable(false);

        TCNameField.setText(user.getTCName());
        TCNameField.setEditable(false);

        TSexField.setText(user.getTSex());
        TSexField.setEditable(false);

        JTnoField.setText(user.getJTno());
        JTnoField.setEditable(false);

        TQualificationField.setText(user.getTQualification());
        TQualificationField.setEditable(false);

        TTnoField.setText(user.getTTno());
        TTnoField.setEditable(false);

        TDutiesField.setText(user.getTDuties());
        TDutiesField.setEditable(false);

        TSituationField.setText(user.getTSituation());
        TSituationField.setEditable(false);

        TEducationField.setText(user.getTEducation());
        TEducationField.setEditable(false);

        TFirstDisciplineField.setText(user.getTFirstDiscipline());
        TFirstDisciplineField.setEditable(false);

        TSecondDisciplineField.setText(user.getTSecondDiscipline());
        TSecondDisciplineField.setEditable(false);

        TProfessionalAffiliationsField.setText(user.getTProfessionalAffiliations());
        TProfessionalAffiliationsField.setEditable(false);

        TAdminPositionsCollegeField.setText(user.getTAdminPositionsCollege());
        TAdminPositionsCollegeField.setEditable(false);

        TAcademicPositionsCollegeField.setText(user.getTAcademicPositionsCollege());
        TAcademicPositionsCollegeField.setEditable(false);

        TRemarkField.setText(user.getTRemark());
        TRemarkField.setEditable(false);

        TPasswordField.setText(user.getTPassword());
        TPasswordField.setEditable(false);

    }

    public void backButtonClicked() {
        NavigationController.back();
    }

    public void logoutButtonClicked() {
        MainController.logout();
        MainPageViewModel.updateHelloText();
        NavigationController.back();
    }

    public void editButtonClicked() {
        // 在编辑按钮点击时启用文本框
        TPasswordField.setEditable(true);
    }
    public void saveButtonClicked() {
        //完成数据库更新
        String Tno = TnoField.getText();
        String TPassword = TPasswordField.getText();

        try {
            TeacherDao.updatePassword(Tno,TPassword);
        } catch (SQLException e) {
            e.printStackTrace();
        }

        // 在保存按钮点击后禁用文本框
        TPasswordField.setEditable(false);
    }
}